if(!window.NW3){
/**
* @version v 0.0.0 2009/10/01
* @package Nerv Widgets, nw.js
* @subpackage Nerv Form Reminder, formreminder.js
* @author Manuele Castellan <manuele.castellan@gmail.com>
*
* @copyright    Copyright (C) Copyright (c) 2009 Manuele Castellan. All rights reserved.
* @license        GNU/GPLv3, see LICENSE
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

 	var NW3 = function(){};
	NW3.version = '000';
	NW3.toString = function(){
		return "Nerv Form Reminder Plugin 0.0.0";
	};
	
	document.write(NW3);
};

NW3.formreminder = {
	formName : null,
	formObj : null,
	formCheckboxId: null,
	classPrefix : 'Nerv-Form-Reminder-',
	cookieDays : 300,
	force : false,
	
	Remind : function(params){
		NW3.formreminder.setParams(params);
		
		if(!NW3.formreminder.force){
			NW3.event.addEvent(NW3.formreminder.formObj, 'submit', function(){
				if(!NW3.formreminder.formCheckboxId || NW3.core.getElement("#"+NW3.formreminder.formCheckboxId).checked){
					NW3.formreminder.SubmitForm();
				}
				else {
					NW3.formreminder.deleteCookieData();
				}
			});
		}
		else{
			NW3.formreminder.SubmitForm();
		}
		
		NW3.formreminder.RemindData();
	},
	
	SubmitForm : function(){
		var data = new Array();
		
		var inputs = NW3.core.getElements("input[class='NWremind']", NW3.formreminder.formObj);
		
		for(var i in inputs){
			switch(inputs[i].getAttribute("type")){
				case 'radio':
				case 'checkbox':
					if( inputs[i].checked ) fieldValue = inputs[i].value;
					break;
				case 'text':
				default:
					fieldValue = inputs[i].value;
					break;
			}
			
			fieldName = inputs[i].getAttribute("name");
			data[fieldName] = fieldValue;
			//NW3.cookie.write(NW3.formreminder.classPrefix + NW3.formreminder.formName + '-' + inputs[i].getAttribute("name"), fieldValue, NW3.formreminder.cookieDays);
		}
		
		
		var selects = NW3.core.getElements("select[class='NWremind']", NW3.formreminder.formObj);
		
		for(var i in selects){
			if( !(fieldValue = selects[i].options[selects[i].selectedIndex].value) ){
				fieldValue = selects[i].options[selects[i].selectedIndex].innerHTML;
			}
			
			fieldName = selects[i].getAttribute("name");
			data[fieldName] = fieldValue;
			//NW3.cookie.write(NW3.formreminder.classPrefix + NW3.formreminder.formName + '-' + selects[i].getAttribute("name"), fieldValue, NW3.formreminder.cookieDays);
		}
		
		
		var textareas = NW3.core.getElements("textarea[class='NWremind']", NW3.formreminder.formObj);
		
		for(var i in textareas){
			var fieldValue = textareas[i].value;
			
			fieldName = textareas[i].getAttribute("name");
			data[fieldName] = fieldValue;
			//NW3.cookie.write(NW3.formreminder.classPrefix + NW3.formreminder.formName + '-' + textareas[i].getAttribute("name"), fieldValue, NW3.formreminder.cookieDays);
		}
		
		
		NW3.cookie.write(NW3.formreminder.classPrefix + NW3.formreminder.formName, NW3.utils.Base64.encode(NW3.utils.Serializer.serialize(data)), NW3.formreminder.cookieDays);
	},
	
	getCookieData : function(){
		var data = NW3.cookie.read(NW3.formreminder.classPrefix + NW3.formreminder.formName);
		data = NW3.utils.Base64.decode(data);
		return NW3.utils.Serializer.unserialize(data);
	},
	
	RemindData : function(params){
		if(params) NW3.formreminder.setParams(params);
		
		var data = NW3.formreminder.getCookieData();
		
		for(var n in data){
			var inputs = NW3.core.getElements("input[name='"+n+"']", NW3.formreminder.formObj);
			
			for(var i in inputs){
				if(NW3.core.object.isObject(inputs[i])){
					switch(inputs[i].getAttribute("type")){
						case 'radio':
						case 'checkbox':
							if(inputs[i].value == data[n]) inputs[i].checked = true;
							break;
						case 'text':
						default:
							inputs[i].value = data[n];
							break;
					}
				}
			}
			
			var select = NW3.core.getElement("select[name='"+n+"']", NW3.formreminder.formObj);
			
			for(s in select.options){
				if(NW3.core.object.isObject(select.options[s])){
					if( data[n] == String(select.options[s].value) ){
						select.options[s].selected = 'selected';
						break;
					}
				}
			}
			
			var textarea = NW3.core.getElement("textarea[name='"+n+"']", NW3.formreminder.formObj);
			
			if(data[n] != 'null') textarea.value = data[n];
		}
	},
	
	deleteCookieData : function(){
		NW3.cookie.erase(NW3.formreminder.classPrefix + NW3.formreminder.formName);
	},
	
	setParams : function(params){
		NW3.formreminder.formName = params.formId;
		NW3.formreminder.formCheckboxId = params.formCheckboxId;
		NW3.formreminder.formObj = NW3.core.getElement("#"+NW3.formreminder.formName);
		NW3.formreminder.force = params.force;
	},
	
	Remove : function(params){
		if(params) NW3.formreminder.setParams(params);
		
		NW3.event.removeEvent(NW3.formreminder.formObj, 'submit', function(){
			NW3.formreminder.SubmitForm();
		});
		
		NW3.event.addEvent(NW3.formreminder.formObj, 'submit', function(){
			NW3.formreminder.deleteCookies();
		});
	}
};